<?php

/**
 * 错误响应
 * @auther yang
 * @param string $msg
 */
function reply_error($msg = 'error')
{
    response()->json([
        'code' => 0,
        'msg' => $msg,
        'data' => []
    ])->send();
    die();
}

/**
 * 响应
 * @auther yang
 * @param array $data
 * @param string $msg
 * @param int $code
 * @return \Illuminate\Http\JsonResponse
 */
function reply($data = [], $msg = '', $code = 1)
{
    response()->json([
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ])->send();
    die();
}

/**
 * post请求
 * @auther yang
 * @param $url
 * @param $data
 * @return bool|string
 */
function curl_post($url, $data)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/**
 * 获取配置信息
 * @auther yang
 * @param $keyword
 * @return mixed
 */
function setting($keyword)
{
    $value = config('system.' . $keyword);//配置文件获取
    if (is_null($value)) {
        $setting = \RedisDB::hmget($keyword, ['value', 'value_type']);//Redis获取
        if (is_null($setting[0]) && is_null($setting[1])) {
            $res = \DB::table('setting')->where(['keyword' => $keyword, 'status' => 1])->first(['value', 'value_type']);//setting表获取
            if ($res) {
                $setting['value'] = $res->value;
                $setting['value_type'] = $res->value_type;
            } else {
                $setting = [];
            }
        } else {
            $setting['value'] = $setting[0];
            $setting['value_type'] = $setting[1];
        }
        if (!empty($setting)) {
            switch ($setting['value_type']) {
                case 1:
                    $value = (string)$setting['value'];
                    break;
                case 2:
                    $value = (int)$setting['value'];
                    break;
                case 3:
                    $value = explode(',', $setting['value']);
                    break;
                case 4:
                    $value = (bool)$setting['value'];
                    break;
                default:
                    $value = (string)$setting['value'];
            }
        } else {
            $value = '';
        }
    }
    return $value;
}

/**
 * 数组转对象
 * @author yang
 * @param object $db  数据库连接
 * @param array $where  条件
 * @return object
 */
function format_sql($db, $where)
{
    if (empty($where)) return $db;
    foreach ($where as $key => $val) {
        if (trim($val[1]) == 'eq') {
            $db = $db->where($val[0], '=', $val[2]);
        } elseif (trim($val[1]) == 'neq') {
            $db = $db->where($val[0], '<>', $val[2]);
        } elseif (trim($val[1]) == 'like') {
            $db = $db->where($val[0], 'like', $val[2]);
        } elseif (trim($val[1]) == 'in') {
            $db = $db->whereIn($val[0], $val[2]);
        } elseif (trim($val[1]) == 'not in') {
            $db = $db->whereNotIn($val[0], $val[2]);
        } elseif (trim($val[1]) == 'between') {
            $db = $db->whereBetween($val[0], $val[2]);
        } elseif (trim($val[1]) == 'not between') {
            $db = $db->whereNotBetween($val[0], $val[2]);
        } elseif (trim($val[1]) == 'egt') {
            $db = $db->where($val[0], '>=', $val[2]);
        } elseif (trim($val[1]) == 'elt') {
            $db = $db->where($val[0], '<=', $val[2]);
        } elseif (trim($val[1]) == 'gt') {
            $db = $db->where($val[0], '>', $val[2]);
        } elseif (trim($val[1]) == 'lt') {
            $db = $db->where($val[0], '<', $val[2]);
        } elseif (trim($val[0]) == 'order') {
            $db = $db->orderBy($val[1], $val[2]);
        } elseif (trim($val[0]) == 'group') {
            $db = $db->groupBy($val[1]);
        } elseif (trim($val[0]) == 'sql_where') {
            $db = $db->whereRaw($val[1], $val[2]);
        }
    }
    return $db;
}

/**
 * 分页
 * @auther yang
 * @param int $page_num
 * @param int $page_size
 * @return int
 */
function page_limit(int $page_num = 1, int $page_size = 20)
{
    $result = ($page_num - 1) * $page_size;
    return $result;
}

/**
 * 效验参数是否存在
 * @auther yang
 * @param array $params
 * @param array $check
 * @return bool
 */
function isEmptyParams($params, $check_params)
{
    $filter_params = array_intersect_key($check_params, $params);
    if (count($filter_params) != count($check_params)){
        reply_error('参数缺失');
    }
    return true;
}

/**
 * 获取指定参数,
 * @auther yang
 * @param array $params
 * @param array $get_params
 * @param int $is_param 参数值不存在时 0不设置默认值 1设置默认值
 * @param int $is_errer 参数不存在时 0不返回错误 1返回错误
 * @return array
 */
function getParams($params, $get_params, $is_param = 0, $is_errer = 0)
{
    // 如果不是关联数组,可以不传默认值
    if (!empty($get_params[0])) {
        $get_params = array_flip($get_params);
    }

    $filter_params = array_intersect_key($params, $get_params);
    if ($is_errer === 1) {
        if (count($filter_params) != count($get_params)) {
            $err_param = array_diff_key($get_params, $filter_params);
            reply_error('参数缺失: '.key($err_param));
        }
    }

    $data = $filter_params;
    if ($is_param == 1) {
        foreach ($filter_params as $key=>$value) {
            if (empty($value)) {
                $data[$key] = $get_params[$key];
            }
        }
    }

    return $data;
}

/**
 * crmeb相关请求
 * @auther yang
 * @param int $code
 * @param array $data
 * @param string $other
 * @param string $req_method
 * @return array|bool|string
 */
function crmeb_curl($code, $data, $other = '', $req_method = 'post')
{
    $url = env('CRMEB_URL').'/admin/';
    switch ($code)
    {
        case 1: # 发货接口
            $url .= 'order.store_order/update_delivery'.$other;
            break;
        case 2: # 退款接口
            $url .= 'order.store_order/updateRefundY'.$other;
            break;
        case 3: # 修改订单
            $url .= 'order.store_order/update'.$other;
            break;
        case 4: # 确认收货
            $url .= 'order.store_order/take_delivery'.$other;
            break;
        case 5: # 添加商品
            $url .= 'store.store_product/save'.$other;
            break;
        case 6: # 修改商品
            $url .= 'store.store_product/update'.$other;
            break;
        case 7: # 图片上传
            $url .= 'store.store_product/image'.$other;
            break;
        default:
            return ['code'=> 0, 'msg'=>'参数错误', 'data'=>[]];
    }

    $appKey = 'IuxcPiSw4gnpvBfSfvuEklT3o7tdgVW2';
    $data['app_key'] = $appKey;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    $res = curl_exec($curl);
    curl_close($curl);
    return json_decode($res, true);
}

